home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / linuxdoc-sgml-1.1 / sgmls-1.1 / genlex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.9 KB  |  115 lines

  1. /* genlex: Generate lexical tables for non-ASCII charsets. */
  2.  
  3. #include "config.h"
  4. #include "std.h"
  5. #include "tools.h"
  6.  
  7. #define CANON_ASCII_NONSGML 255  /* Canonical non-SGML character in ASCII. */
  8. #define CANON_ASCII_DATACHAR 254 /* Canonical DATACHAR in ASCII. */
  9.  
  10. extern unsigned char charset[];
  11. extern UNCH *lextabs[];
  12. extern UNCH lextran[];
  13.  
  14. static char *lextabnames[] = {
  15.      "lexcnm", "lexcon", "lexgrp", "lexlms", "lexmark", "lexsd", "lextoke"
  16. };
  17.  
  18. static VOID print_tab(s, t)
  19.      char *s;
  20.      UNCH *t;
  21. {
  22.   int i;
  23.   printf("UNCH %s[] = {\n", s);
  24.   for (i = 0; i < 256; i++)
  25.     printf("%2d,%c", t[i], (i + 1) % 16 == 0 ? '\n' : ' ');
  26.   fputs("};\n\n", stdout);
  27. }
  28.  
  29. int main(argc, argv)
  30.      int argc;
  31.      char **argv;
  32. {
  33.   int i;
  34.   UNCH tab[256];
  35.   char special[256];
  36.   /* Shunned character numbers in the reference concrete syntax. */
  37.   static UNCH refshun[] = { 
  38.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  39.     19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 255
  40.   };
  41.   char shunned[256];
  42.   char *program_name;
  43.  
  44.   program_name = strrchr(argv[0], '/');
  45.   if (program_name)
  46.     program_name++;
  47.   else
  48.     program_name = argv[0];
  49.  
  50.   /* Check that the mapping is 1-1. */
  51.   for (i = 0; i < 256; i++)
  52.     tab[i] = 0;
  53.   for (i = 0; i < 256; i++)
  54.     tab[charset[i]] = 1;
  55.   for (i = 0; i < 256; i++)
  56.     if (!tab[i]) {
  57.       fprintf(stderr, "%s: bad mapping: no character mapped to %d\n",
  58.           program_name, i);
  59.       exit(EXIT_FAILURE);
  60.     }
  61.  
  62.   /* Compute special. */
  63.   for (i = 0; i < 256; i++)
  64.     special[i] = 0;
  65.   for (i = 0; lextabs[i]; i++) {
  66.     int j;
  67.     for (j = 0; j < 256; j++)
  68.     if (lextabs[i][j] != lextabs[i][CANON_ASCII_NONSGML]
  69.     && lextabs[i][j] != lextabs[i][CANON_ASCII_DATACHAR])
  70.       special[charset[j]] = 1;
  71.   }
  72.  
  73.   /* Compute shunned. */
  74.   for (i = 0; i < 256; i++)
  75.     shunned[i] = 0;
  76.   for (i = 0; i < sizeof(refshun); i++)
  77.     shunned[refshun[i]] = 1;
  78.  
  79.   printf("/* This file was automatically generated by %s.  Do not edit. */\n\n",
  80.     program_name);
  81.   fputs("#include \"config.h\"\n#include \"entity.h\"\n#include \"sgmldecl.h\"\n\n",
  82.     stdout);
  83.  
  84.   /* Generate each of the lexical tables. */
  85.   for (i = 0; lextabs[i]; i++) {
  86.     int j;
  87.     for (j = 0; j < 256; j++)
  88.       tab[charset[j]] = lextabs[i][j];
  89.  
  90.     for (j = 0; j < 256; j++)
  91.       if (!special[j]) {
  92.     if (shunned[j]) 
  93.       tab[j] = lextabs[i][CANON_ASCII_NONSGML];
  94.     else
  95.       tab[j] = lextabs[i][CANON_ASCII_DATACHAR];
  96.       }
  97.     print_tab(lextabnames[i], tab);
  98.   }
  99.  
  100.   /* Generate lextran. */
  101.   for (i = 0; i < 256; i++)
  102.     tab[charset[i]] = charset[lextran[i]];
  103.   print_tab("lextran", tab);
  104.  
  105.   /* Generate asciicharset. */
  106.   fputs("int asciicharset[] = {\n", stdout);
  107.   for (i = 0; i < 128; i++)
  108.     printf("%3d,%c", charset[i], (i + 1) % 16 == 0 ? '\n' : ' ');
  109.   for (i = 128; i < 256; i++)
  110.     printf("UNUSED,%c", (i + 1) % 8 == 0 ? '\n' : ' ');
  111.   fputs("};\n", stdout);
  112.  
  113.   exit(EXIT_SUCCESS);
  114. }
  115.